package com.example.demo.controller;

import com.example.demo.dao.db2.ArcheryDao;
import com.example.demo.dao.db1.UserDao;
import com.example.demo.entity.ArcheryUser;
import com.example.demo.entity.ErpUser;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

@Slf4j
@Controller
@PropertySource("classpath:conf/cron.props")
@MapperScan("com.example.demo.dao") //mapper扫描
public class LoginController {

    @Autowired
    private UserDao userDao;
    @Autowired
    private ArcheryDao archeryDao;
    /***
     * the main code which transfer erp user to archery user
     */
    @Scheduled(cron = "${archery.schedule}")
    public void synUser() {
        Date now = new Date();
        log.info("SyncUser is executing>>>>>" + now.toString());
        try{
            //新入职用户,执行新增；
            List<ErpUser> erpUserList = userDao.selectUsers();
            System.out.println("the list size is===:"+erpUserList.size());
            for(ErpUser eUser:erpUserList){
                ArcheryUser archeryUser = new ArcheryUser();
                archeryUser.setId(eUser.getAccountid());
                archeryUser.setPassword("pbkdf2_sha256$150000$btwwjIeDl5Ml$fBNY5QlcDmepfhR+O7X/CtMLwepVcEQfsp8jLlamEGA=");
                archeryUser.setIsSuperuser(0);
                archeryUser.setUserName(eUser.getAccountNo());
                archeryUser.setFirstName("");
                archeryUser.setLastName("");
                archeryUser.setEmail(eUser.getEmail()==null?"":eUser.getEmail());
                archeryUser.setIsStaff(1);
                archeryUser.setIsActive(1);
                archeryUser.setDateJoined(now);
                archeryUser.setDisplay(eUser.getName());
                archeryUser.setDingUserId(eUser.getUserid());
                archeryUser.setWxUserId("");
                archeryUser.setFeishuOpenId("");
                archeryUser.setFailedLoginCount(0);
                archeryDao.insertUser(archeryUser);
//                archeryDao.insertUGroup(eUser.getAccountid(),Integer.parseInt(eUser.getDeptid()));
//                archeryDao.insertRGroup(eUser.getAccountid(),Integer.parseInt(eUser.getDeptid()));
            }
            //同步离职用户
            List<ErpUser> leaveUserList = userDao.selectLeaveUsersByTime(now.getTime()-86400*1000L);
            for(ErpUser eUser:leaveUserList){
                System.out.println(eUser.getName()+"||"+eUser.getAccountNo()+"||"+eUser.getAccountid());
                archeryDao.updateUser(eUser.getAccountid());
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }


    /***
     * The main code which update dingInfo to archery user
     */
//    @Scheduled(cron = "${archery.schedule}")
    public void updateArcheryUser() {
        Date now = new Date();
        log.info("SyncUser is executing>>>>>" + now.toString());
        try{
            //执行新增；
            List<ErpUser> erpUserList = userDao.selectAllUsers();
            System.out.println("the list size is===:"+erpUserList.size());
            for(ErpUser eUser:erpUserList){
                String accountNo=eUser.getAccountNo();
                String dingUid = eUser.getUserid();
                archeryDao.updateDingUser(dingUid,accountNo);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    @ResponseBody
    @RequestMapping(value = "/hello")
    public void hello(HttpServletResponse response) throws InterruptedException{
//        this.synUser();
        try {
            response.getWriter().print("welcome access hello");
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("Hello==========WWWWSSSSSSS");
    }

}